package lab03;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class TopPassword {
    private static final String SQL = "select password from db_csdn.csdn";

    public static void main(String[] args) throws Exception {
        Connection connection = DBCPDatasource.getConnection();
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(SQL);
        ResultSet resultSet = preparedStatement.executeQuery(SQL);
        List<String> lists = new ArrayList<>();
        while(resultSet.next()){
            String pass = resultSet.getString(1);
            lists.add(pass);
        }

        Map<String,Integer> maps = new HashMap<>();
        for (String list : lists) {
            Integer count = maps.get(list);
            maps.put(list,(count == null) ? 1 : count + 1);

        }

        List<Map.Entry<String,Integer>> arrs = new ArrayList<>(maps.entrySet());
        Collections.sort(arrs, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        List<Map.Entry<String, Integer>> lastlist = arrs.subList(0, 10);
        System.out.println(lastlist);
        System.out.println("密码：\t\t\t 数量");

        for (Map.Entry<String, Integer> res : lastlist) {
            System.out.println(res.getKey() + " \t\t\t " +res.getValue());
        }

        preparedStatement.executeBatch();
        connection.commit();
        preparedStatement.close();
        connection.close();
    }
}

/*密码：			 数量
123456789 			 235012
12345678 			 212749
11111111 			 76346
dearbook 			 46053
00000000 			 34952
123123123 			 19986
1234567890 			 17790
88888888 			 15033
111111111 			 6995
147258369 			 5965

Process finished with exit code 0
*/
